package com.itshare.video.controller;

import com.github.pagehelper.PageHelper;
import com.itshare.video.common.annontation.AttrData;
import com.itshare.video.common.annontation.Page;
import com.itshare.video.common.annontation.Size;
import com.itshare.video.common.core.constant.enums.ApiInfo;
import com.itshare.video.common.dto.PageEntity;
import com.itshare.video.common.dto.Result;
import com.itshare.video.pojo.bo.FreezeAccountBO;
import com.itshare.video.pojo.bo.VideoPermitBO;
import com.itshare.video.pojo.dto.UserTokenDTO;
import com.itshare.video.pojo.po.Video;
import com.itshare.video.service.ManagerService;
import com.itshare.video.service.UserMessageService;
import com.itshare.video.service.UserService;
import com.itshare.video.service.VideoService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.time.Duration;
import java.util.List;

/**
 * @Author lyr
 * @create 2020/10/31 18:43
 */
@RestController
@RequestMapping("/api")

public class ManagerController {
    @Resource
    private ManagerService managerService;
    @Resource
    private UserService userService;
    @Resource
    private VideoService videoService;
    @Resource
    private UserMessageService userMessageService;


    /**
     * 获取未审核的视频
     * @param page
     * @param size
     * @return
     */
    @RequiresPermissions("manager:video:all")
    @GetMapping("/new_video/all")
    public Result<?> getAllVideoToBePublish(@Page Integer page,@Size Integer size) {
        PageHelper.startPage(page,size);
        List<Video> res = managerService.getAllVideoInfoNotPermited();
        return Result.of(ApiInfo.OK)
                .setData(PageEntity.of(res));
    }

    /**
     * 审核视频通过
     *
     * @param videoId
     * @param videoPermitBO
     * @param userTokenDTO
     * @return
     */
    @PutMapping("/video_permit")
    @RequiresPermissions("manager:video:all")
    public Result<?> setVideoPermit(@RequestParam Long videoId,
                                    @RequestBody @Validated VideoPermitBO videoPermitBO,
                                    @AttrData UserTokenDTO userTokenDTO) {
        return managerService.setVideoPermited(videoId,videoPermitBO,userTokenDTO);

    }


    //todo: 对接
    /**
     * 必须是 管理员才能动用这个接口
     * @param freezeAccountBO
     * @return
     */
    @RequiresPermissions("manager:video:all")
    @PutMapping("/user_account_freeze")
    public Result<?> setUserAccountFreeze(@RequestBody @Validated FreezeAccountBO freezeAccountBO) {
        //冻结账户
        return userService.setUserAccountFreezeForUpload(freezeAccountBO.getUserId(), Duration.ofDays(freezeAccountBO.getDay()));
    }

    @RequiresPermissions("manager:video:all")
    @PutMapping("/del_video")
    public Result deleteVideo(@RequestParam Integer videoId,
                              @RequestParam Integer messageId) {
        //通知举报通过
        userMessageService.sendFeedbackMessageOK(messageId);

        return videoService.logicDeleteVideo(videoId);
    }


    /**
     * 举报不通过
     * @param messageId
     * @return
     */
    @PostMapping("/feedback_userInfo")
    public Result reportFeedback(@RequestParam Integer messageId) {
        //用户举报不成立，通知他
        userMessageService.handleFeedbackNoPass(messageId);
        return Result.of(ApiInfo.OK);
    }


    /**
     * 获取被举报了的视频
     * @return
     */
    @GetMapping("video_reported")
    public Result<?> getALLVideoBeReported(@Page Integer page,@Size Integer size) {
        return videoService.getAllVideoBeReported(page,size);
    }



}
